import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:fantuike_app/api/Api.dart' show Api;
import 'child_page/notice_detail.dart';

class NoticeListPage extends StatefulWidget {
  @override
  _NoticeListPageState createState() => _NoticeListPageState();
}

class _NoticeListPageState extends State<NoticeListPage> {
  PreferredSize buildAppBarWidget() {
    return PreferredSize(
        child: AppBar(
          leading: IconButton(
              icon: Icon(
                Icons.chevron_left,
                size: 30.0,
                color: Color(0xffffffff),
              ),
              onPressed: () {
                Navigator.pop(context);
              }),
          backgroundColor: Color(0xfff26966),
          title: Text(
            "系统公告",
            style: TextStyle(
                color: Color(
                  0xffffffff,
                ),
                fontSize: 18.0),
          ),
          centerTitle: true,
          elevation: 0.0,
        ),
        preferredSize:
            Size.fromHeight(MediaQuery.of(context).size.height * 0.065));
  }

  List noticeList = [];

  @override
  void initState() {
    super.initState();
    getNoticeList();
  }

  void getNoticeList() {
    _getNoticeList().then((data) {
      setState(() {
        noticeList.addAll(data['extend']['message']['list']);
      });
    });
  }

  Future _getNoticeList() async {
    try {
      Response response = await Dio().get(Api.getSysNoticeList);
      return response.data;
    } catch (e) {}
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> tiles = [];

    for (var item in noticeList) {
      tiles.add(GestureDetector(
        onTap: () {
          print(item['id']);
          Navigator.of(context).push(MaterialPageRoute(
              builder: (context) => NoticeDetail(noticeId: item['id'])));
        },
        child: new SizedBox(
          height: 100.0, //设置高度
          child: new Card(
//              margin: const EdgeInsets.only(top: 10.0,left: 10.0,right: 10.0),
              elevation: 3.0, //设置阴影
              shape: const RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(14.0))), //设置圆角
              child: Container(
                margin: const EdgeInsets.only(
                    top: 10.0, left: 10.0, right: 10.0, bottom: 20.0),
                child: Row(
                  children: <Widget>[
                    Image.network(
                      item['avatar'],
                      fit: BoxFit.fill,
                    ),
                    Text(item['title'])
                  ],
                ),
              )),
        ),
      ));
    }

    return Scaffold(
      appBar: buildAppBarWidget(),
      body: SingleChildScrollView(
        child: ListView(
          physics: NeverScrollableScrollPhysics(),
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          padding: const EdgeInsets.all(5.0),
          children: tiles,
        ),
      ),
    );
  }
}
